﻿// Type: GandA.Component.Security.Permisos
// Assembly: GandA.Component.Security, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// Assembly location: C:\CodePlex\CLab\CLab\DLLs\GandA.Component.Security.dll

using System;
using System.Configuration;
using System.Configuration.Provider;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;

namespace GandA.Component.Security
{
    public class Permisos
    {
        private string connectionString;

        public Permisos(string _connectionString)
        {
            ConnectionStringSettings connectionStringSettings = ConfigurationManager.ConnectionStrings[_connectionString];
            if (connectionStringSettings == null || connectionStringSettings.ConnectionString.Trim() == "")
                throw new ProviderException("Connection string cannot be blank.");
            this.connectionString = connectionStringSettings.ConnectionString;
        }

        public PermisoModulo ObtenModulo(string userName)
        {
            SqlConnection connection = new SqlConnection(this.connectionString);
            SqlCommand sqlCommand = new SqlCommand("security.spObtenPerfilModulo", connection);
            sqlCommand.CommandType = CommandType.StoredProcedure;
            sqlCommand.Parameters.Add("@psUserName", SqlDbType.VarChar).Value = (object)userName;
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
            DataSet dataSet = new DataSet();
            PermisoModulo permisoModulo = new PermisoModulo();
            try
            {
                connection.Open();
                sqlDataAdapter.SelectCommand = sqlCommand;
                ((DataAdapter)sqlDataAdapter).Fill(dataSet);
                if (dataSet.Tables.Count > 0)
                {
                    foreach (DataRow dataRow in (InternalDataCollectionBase)dataSet.Tables[0].Rows)
                    {
                        permisoModulo.IdModulo = Convert.ToInt32(dataRow["IdModulo"]);
                        permisoModulo.NombreModulo = dataRow["NombreModulo"].ToString();
                    }
                }
                return permisoModulo;
            }
            catch (Exception ex)
            {
                throw new ProviderException(ex.Message);
            }
            finally
            {
                if (connection.State == ConnectionState.Open)
                    connection.Close();
                connection.Dispose();
                dataSet.Dispose();
            }
        }

        public Perfil ObtenPerfil(string userName)
        {
            SqlConnection connection = new SqlConnection(this.connectionString);
            SqlCommand sqlCommand = new SqlCommand("security.spObtenPerfilModulo", connection);
            sqlCommand.CommandType = CommandType.StoredProcedure;
            sqlCommand.Parameters.Add("@psUserName", SqlDbType.VarChar).Value = (object)userName;
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
            DataSet dataSet = new DataSet();
            Perfil perfil = new Perfil();
            try
            {
                connection.Open();
                sqlDataAdapter.SelectCommand = sqlCommand;
                ((DataAdapter)sqlDataAdapter).Fill(dataSet);
                if (dataSet.Tables.Count > 0)
                {
                    foreach (DataRow dataRow in (InternalDataCollectionBase)dataSet.Tables[1].Rows)
                    {
                        perfil.IdPerfil = Convert.ToInt32(dataRow["IdPerfil"]);
                        perfil.NombrePerfil = dataRow["NombrePerfil"].ToString();
                        perfil.IdEmpresa = Convert.ToInt32(dataRow["IdEmpresa"]);
                        perfil.UserName = dataRow["UserName"].ToString();
                        perfil.IdUsuario = Convert.ToInt32(dataRow["IdUsuario"]);
                        perfil.Estatus = Convert.ToInt32(dataRow["IdEstatus"]);
                        perfil.NombreEmpresa = dataRow["Nombre"].ToString();
                        perfil.IdSucursal = Convert.ToInt32(dataRow["IdSucursal"]);
                        perfil.Tema = dataRow["Tema"].ToString();
                    }
                }
                return perfil;
            }
            catch (Exception ex)
            {
                throw new ProviderException(ex.Message);
            }
            finally
            {
                if (connection.State == ConnectionState.Open)
                    connection.Close();
                connection.Dispose();
                dataSet.Dispose();
            }
        }

        public PermisoItem ObtenPermisoItem(int idItem, Perfil perfil)
        {
            SqlConnection connection = new SqlConnection(this.connectionString);
            SqlCommand sqlCommand = new SqlCommand("security.spObtenPermisoItem", connection);
            sqlCommand.CommandType = CommandType.StoredProcedure;
            sqlCommand.Parameters.Add("@pnIdPerfil", SqlDbType.VarChar).Value = (object)perfil.IdPerfil;
            sqlCommand.Parameters.Add("@pnIdItem", SqlDbType.VarChar).Value = (object)idItem;
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
            DataSet dataSet = new DataSet();
            PermisoItem permisoItem = new PermisoItem();
            try
            {
                connection.Open();
                sqlDataAdapter.SelectCommand = sqlCommand;
                ((DataAdapter)sqlDataAdapter).Fill(dataSet);
                if (dataSet.Tables.Count > 0)
                {
                    foreach (DataRow dataRow in (InternalDataCollectionBase)dataSet.Tables[0].Rows)
                    {
                        permisoItem.IdPerfil = Convert.ToInt32(dataRow["IdPerfil"]);
                        permisoItem.IdItem = Convert.ToInt32(dataRow["IdItem"]);
                        permisoItem.PermisoAcceso = Convert.ToInt32(dataRow["PermisoAcceso"]);
                        permisoItem.PermisoAdicional1 = Convert.ToInt32(dataRow["PermisoAdicional1"]);
                        permisoItem.PermisoAdicional2 = Convert.ToInt32(dataRow["PermisoAdicional2"]);
                        permisoItem.PermisoAdicional3 = Convert.ToInt32(dataRow["PermisoAdicional3"]);
                        permisoItem.PermisoAdicional4 = Convert.ToInt32(dataRow["PermisoAdicional4"]);
                        permisoItem.PermisoAdicional5 = Convert.ToInt32(dataRow["PermisoAdicional5"]);
                        permisoItem.PermisoAdicional6 = Convert.ToInt32(dataRow["PermisoAdicional6"]);
                        permisoItem.PermisoAdicional7 = Convert.ToInt32(dataRow["PermisoAdicional7"]);
                        permisoItem.PermisoAdicional8 = Convert.ToInt32(dataRow["PermisoAdicional8"]);
                        permisoItem.PermisoAdicional9 = Convert.ToInt32(dataRow["PermisoAdicional9"]);
                        permisoItem.PermisoAdicional10 = Convert.ToInt32(dataRow["PermisoAdicional10"]);
                    }
                }
                return permisoItem;
            }
            catch (Exception ex)
            {
                throw new ProviderException(ex.Message);
            }
            finally
            {
                if (connection.State == ConnectionState.Open)
                    connection.Close();
                connection.Dispose();
                dataSet.Dispose();
            }
        }
    }
}
